package com.zhilianbao.leyaogo.utils;

import android.app.Activity;
import android.content.Context;

import com.tencent.mm.sdk.constants.Build;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;
import com.zhilianbao.leyaogo.model.response.shoppingcart.WXPay;

/**
 * ClassName: WXPayUtils <br>
 * Description: 微信支付相关<br>
 * Author: Cyarie <br>
 * Created: 2016/12/8 11:55 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class WXPayUtils {


    /**
     * 检测是否支持微信支付
     */
    public static boolean checkIsSupportWXPay(Context context) {
        if (context instanceof Activity) {
            boolean isPaySupported = LeYaoGoApplication.sApi.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
            boolean isWXInstalled = LeYaoGoApplication.sApi.isWXAppInstalled();
            if (!isWXInstalled) {//没有安装微信客户端
                XToastUtils.showShortToast(context.getString(R.string.wx_pay_not_installed));
                return false;
            } else if (!isPaySupported) {//微信客户端版本过低
                XToastUtils.showShortToast(context.getString(R.string.wx_pay_not_support));
                return false;
            }

            //            if (!isPaySupported) {//微信客户端版本过低
            //                //                XTipsDialog.showFail(context, context.getString(R.string.wx_pay_not_support));
            //                XToastUtils.showShortToast(context.getString(R.string.wx_pay_not_support));
            //                return false;
            //            } else if (!isWXInstalled) {//没有安装微信客户端
            //                //                XTipsDialog.showFail(context, context.getString(R.string.wx_pay_not_installed));
            //                XToastUtils.showShortToast(context.getString(R.string.wx_pay_not_installed));
            //                return false;
            //            }
        } else {
            throw new IllegalArgumentException("context must be instanceof Activity");
        }
        return true;
    }


    /**
     * 微信支付
     */
    public static void payByWX(WXPay wxPay) {
        PayReq req = new PayReq();
        //        req.appId = wxPay.getAppId();
        req.appId = wxPay.getAppId();
        req.partnerId = wxPay.getPartnerId();
        req.prepayId = wxPay.getPrePayId();
        req.nonceStr = wxPay.getNonceStr();
        req.timeStamp = wxPay.getTimestamp();
        req.packageValue = "Sign=WXPay";
        req.sign = wxPay.getSign();//签名后台返回，签名看在本地还是服务器
        //        req.extData = "app data";
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        LeYaoGoApplication.sApi.sendReq(req);//支付结果在.wxapi.WXPayEntryActivity
    }
}
